<?php

/**
 * uniBoard ACP Blacklist User Titles
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_blist_titles{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Add language string
		$appCore -> output -> js_vars['l_delete_item_sure'] =  $appCore -> lang['acp_blacklist_item_sure'];
		$appCore -> output -> js_vars['l_delete_selected_items_sure'] =  $appCore -> lang['acp_blacklist_items_sure'];
			
		// Load fields templates
		$appCore -> templates['form_inputs'] = new tpl_form_inputs( $appCore);
		
		// Delete Disallow
		if ( $appCore -> _REQ( 'do') == 'delete')
		{
			// Get Disallow API
			$dissallow_api = new api_disallow( $appCore, $appCore -> _REQ( 'item'));
			
			// Found it?
			if ( $dissallow_api -> data_loaded && strlen( $dissallow_api -> disallow_title) > 0)
			{
				// Delete
				$dissallow_api -> deleteData();
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_blisted_user_title_removed', htmlspecialchars( $dissallow_api -> disallow_title));
				
				// Draw message
				$appCore -> output -> message( $appCore -> lang['acp_blist_title_deleted'], $appCore -> lang['acp_disallow_titles']);
			}
			else
			{
				// Draw message
				$appCore -> output -> error( $appCore -> lang['acp_disallow_not_found'], $appCore -> lang['acp_disallow_titles']);
			}
		}
		
		// Delete many Items
		if ( $appCore -> _POST( 'do') == 'mass_delete')
		{
			// Selected Items
			$items = $appCore -> listInput( 'select_item');
			
			// Anything selected?
			if ( empty( $items))
			{
				// Draw message
				$appCore -> output -> error( $appCore -> lang['nothing_selected'], $appCore -> lang['acp_disallow_titles']);
			}
			else
			{
				// Delete data
				$appCore -> db -> delete( 'disallow_names', 'disallow_id IN (' . join( ',', $items) . ') AND disallow_title <> \'\'');
				
				// Flush Cache
				class_cache::flushCache( 'disallow_titles');
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_blisted_user_titles_removed');
				
				// Draw message
				$appCore -> output -> message( $appCore -> lang['acp_blist_titles_deleted'], $appCore -> lang['acp_disallow_titles']);
			}
		}
		
		// Cache output
		ob_start();
		
		// New Ban Button
		echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=new_blist_title'));
		echo $appCore -> output -> openFormBlock( $appCore -> lang['new_blacklist_title_form']);
		echo $appCore -> output -> submitButton( $appCore -> lang['new_blacklist_title_form_submit']);
		echo $appCore -> output -> closeFormBlock();
		echo $appCore -> output -> closeForm();
		
		// Search Bans
		echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=blist_titles'));
		echo $appCore -> output -> openFormBlock( $appCore -> lang['search_blacklist_form'],  $appCore -> lang['search_bans_form_match']);
		echo $appCore -> output -> formItem( $appCore -> lang['search_blacklist_form_user_title'], $appCore -> drawTextInput( 'find', $appCore -> _REQ( 'find')));
		echo $appCore -> output -> submitButton( $appCore -> lang['search_blacklist_form_submit']);
		echo $appCore -> output -> closeFormBlock();
		echo $appCore -> output -> closeForm();
		
		// Build search query
		$search_sql = 'disallow_title <> \'\'';
		
		// User Name
		if ( strlen( $appCore -> _REQ( 'find')) > 0)
		{
			$search_sql .= ' AND disallow_title LIKE \'' . str_replace( '*', '%', $appCore -> db -> strEscape( $appCore -> _REQ( 'find'), true)) . '\'';
		}
		
		// Count Items to Display
		$items_number = $appCore -> db -> count( 'disallow_names', $search_sql);
		
		// We will Display 20 logs per page
		$paginator = new class_paginator( $appCore, $appCore -> adminLink( 'act=blist_titles&st=%s&find=' . class_strings::sprintfEnc( $appCore -> _REQ( 'find'))), $items_number, 15);
		
		// Draw Paginator
		$paginator_html = $paginator -> draw();
				
		// Display Paginator
		echo $paginator_html;
		
		// Open table block
		echo $appCore -> output -> openTableBlock( $appCore -> lang['found_bans_form'] . ': ' . $appCore -> number( $items_number));
				
		// Select Bans
		$appCore -> db -> query( 'SELECT disallow_id, disallow_title
		FROM ' . DB_PREFIX . 'disallow_names
		WHERE ' . $search_sql . '
		ORDER BY disallow_name ASC
		LIMIT ' . $paginator -> getPosition() . ',15');
		
		// Draw header
		echo $appCore -> output -> tableHeader( array(
			array(
				'left_float width_big',
				$appCore -> lang['column_user_name']
			),
			array(
				'right_float width_tiny center',
				( $appCore -> db -> numRows() > 0 ? $appCore -> drawCheckbox( '', false, 'checkbox_master') : '')
			),
			array(
				'right_float width_medium',
				$appCore -> lang['actions']
			)
		));		
		
		// Open Mass-Delete forum?
		if ( $appCore -> db -> numRows() > 0)
		{
			// Open form
			echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=blist_titles'));
			echo $appCore -> drawHidden( 'do', 'mass_delete');
		}
		else
		{
			// Draw Row with "no bans found" message
			echo $appCore -> output -> tableRow('
				<div class="cell">
					' . $appCore -> lang['acp_disallows_not_found'] . '	
				</div>');
		}
		
		// Parse result
		while ( $disallow_result = $appCore -> db -> fetch_assoc())
		{
			// Draw row
			echo $appCore -> output -> tableRow('
				<div class="cell left_float width_quarter">
					<b>' . htmlspecialchars( $disallow_result['disallow_title']) . '</b>
				</div>
				<div class="cell left_border right_float width_tiny center">
					' . $appCore -> drawCheckbox( 'select_item[' . $disallow_result['disallow_id'] . ']', false, 'checkbox_children') . '
				</div>
				<div class="cell left_border right_float width_medium center">
					<a href="' . $appCore -> adminLink( 'act=blist_titles&do=delete&item=' . $disallow_result['disallow_id']) . '" class="delete_confirm">' . $appCore -> output -> drawImage( 'action_delete', $appCore -> lang['delete']) . '</a>
				</div>');
		}
		
		// Delete Selected Button?
		if ( $appCore -> db -> numRows() > 0)
		{
			// Draw button
			echo $appCore -> output -> submitButton( $appCore -> lang['delete_selected'], '', 'delete_items_confirm');
			
			// Close form
			echo $appCore -> output -> closeForm();
		}
		
		// Free result
		$appCore -> db -> freeResult();
		
		// Close table block
		echo $appCore -> output -> closeTableBlock();
				
		// Display Paginator
		echo $paginator_html;
		
		// Draw output
		$appCore -> output -> draw( $appCore -> lang['acp_disallow_titles'], array( $appCore -> adminLink( 'act=blist_titles') => $appCore -> lang['acp_disallow_titles']), ob_get_clean());
				
	}

}